package com.gcxy.shop.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gcxy.shop.common.CommonException;
import com.gcxy.shop.service.ShopService;
@WebServlet(urlPatterns="/delete.do")
public class ShopDeleteServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
   private Logger logger=LoggerFactory.getLogger(getClass());
	
	private ShopService shopService=new ShopService();  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("delete.doGet");
		try {
			//1.接收前台传递过来的参数
			String strId=request.getParameter("id");
			//3.参数合法性校验
			if(strId==null||strId.trim().length()<=0) {
				throw new CommonException("必须传递参数Id才能删除");
			}
			//2.参数类型转换
			Long id=Long.parseLong(strId);
			//4.参数封装为对象
			//5.调用业务方法
			shopService.delete(id);
			//6.转发或重定向(可传递数据给页面)
			response.sendRedirect(request.getContextPath()+"/cart.jsp");
			logger.info("商品删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());//设置属性到请求中
			request.getRequestDispatcher("/cart.jsp"+ "").forward(request, response);
			logger.error("商品删除失败:{}",e.getMessage());
		}
	}
	}

